<?php
/**
 * @ClassName: Contacts
 * @Description: TODO
 * @author: ynbjykj
 * @Date: 2025-11-29 18:01
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\api\controller\agent\Agent;
use app\common\model;

class Contacts extends Api
{
    function getContacts()
    {
        $type = input('type', 'agent');

        switch ($type) {
            case 'agent':
                $dataModel = new model\Agent();
                break;
            case 'master':
                $dataModel = new model\Master();
                break;
            case 'staff':
                $dataModel = new model\StoreStaff();
                break;
            case 'store':
                $dataModel = new model\Store();
                break;
            default:
                $dataModel = new model\Merchant();
                break;
        }

        $data = $dataModel->where('id', $this->user_id)->find();
        if ($data) {
            $result = [
                'type' => $type,
                'contact_name' => $data['contact_name'],
                'contact_phone' => $data['contact_phone'],
            ];
            $code = 1;
            $msg = 'ok';
        } else {
            $result = [
                'type' => $type,
                'contact_name' => '',
                'contact_phone' => '',
            ];
            $code = 0;
            $msg = '系统异常';
        }
        return json(['code' => $code, 'msg' => $msg, 'data' => $result]);
    }

    function saveContacts()
    {
        $type = input('type', 'agent');
        $contact_name = input('contact_name', '');
        $contact_phone = input('contact_phone', '');

        switch ($type) {
            case 'agent':
                $dataModel = new model\Agent();
                break;
            case 'master':
                $dataModel = new model\Master();
                break;
            case 'staff':
                $dataModel = new model\StoreStaff();
                break;
            case 'store':
                $dataModel = new model\Store();
                break;
            default:
                $dataModel = new model\Merchant();
                break;
        }

        $res = $dataModel->where('id', $this->user_id)->update([
            'contact_name' => $contact_name,
            'contact_phone' => $contact_phone,
            'update_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '修改失败']);
        }
        return json(['code' => 1, 'msg' => '修改成功']);
    }

}